/**
 Emma_project
 Copyright (C) 2015  Rodrigo Garcia

 This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "TimeChecker.h"

void CheckForConfiguredTime(struct LogFile *data_log)
{

  DS3231_get_time(&rtc_time); // reads RTC
  
  //current_time_sum = (rtc_time.hour<<16) | (rtc_time.min<<8) | (rtc_time.sec);
  current_time_sum = rtc_time.hour*3600 + rtc_time.min*60 + rtc_time.sec;
  
  //  char aux = DS3231_get_time(&rtc_time);

  /// controls if time interval has passed
  if(current_time_sum < last_time_logged_sum) 
    {  // passed to other day
      //      if((current_time_sum - (1572864-last_time_logged_sum))  >= interval_time_sum) // 24<<16 = 1572864 (24 is hours)
      if((current_time_sum + (86400-last_time_logged_sum)) >= interval_time_sum) // 86400 (24 hours)
	{
	  is_time_to_log = 1;
	  UpdateLastLoggedTime();
	  /*
	   When day has changed, or logging parameters have been changed by user it is mandatory to prepare again what must be logged.
	   change `change_table_header'
	  */
	  UpdateFileNameToCurrentDate(data_log); // sets the FileName to the current date.
	  change_table_header = 1; // to update file table header
	}
      else
	is_time_to_log = 0;
    }
  else
    {
      if((current_time_sum - last_time_logged_sum) >= interval_time_sum)

	{
	  is_time_to_log = 1;
	  UpdateLastLoggedTime();
	}
      else
	is_time_to_log = 0;
    }
}

void UpdateConfiguredTimeFromEEPROM(void)
{
  //interval_time_sum = (EEPROM_read(TIME_INTERVAL_HOUR)<<16)|(EEPROM_read(TIME_INTERVAL_MIN)<<8)|EEPROM_read(TIME_INTERVAL_SEC);
  interval_time_sum = EEPROM_read(TIME_INTERVAL_HOUR)*3600 + EEPROM_read(TIME_INTERVAL_MIN)*60 + EEPROM_read(TIME_INTERVAL_SEC);
}

void UpdateLastLoggedTime(void)
{
  DS3231_get_time(&rtc_time); // reads RTC

  //last_time_logged_sum = rtc_time.hour<<16 | rtc_time.min<<8 | rtc_time.sec;
  last_time_logged_sum = rtc_time.hour*3600 + rtc_time.min*60 + rtc_time.sec;
}

void SetTimeInterval(unsigned char hour, unsigned char min, unsigned char sec)
{
  // writes new time interval to EEPROM
  EEPROM_write(TIME_INTERVAL_SEC, sec);
  EEPROM_write(TIME_INTERVAL_MIN, min);
  EEPROM_write(TIME_INTERVAL_HOUR, hour);

  UpdateConfiguredTimeFromEEPROM();

  // to start sampling right now
  is_time_to_log = 1; 
  UpdateLastLoggedTime();
}

void UpdateFileNameToCurrentDate(struct LogFile *data_log)
{
  char char_num_str[5];

  // TODO: Check for correct response from DS3231
  DS3231_get_time(&rtc_time); // reads RTC
  
  sprintf(char_num_str, "%02d",rtc_time.mday);
  Set_DatalogMetaData(data_log, char_num_str, 0); // DD
  Set_DatalogMetaData(data_log, "-", 1); // DD-
  sprintf(char_num_str, "%02d",rtc_time.mon);
  Set_DatalogMetaData(data_log, char_num_str, 1); // DD-MM
  Set_DatalogMetaData(data_log, "-", 1); // DD-MM-
  sprintf(char_num_str, "%02d",rtc_time.year);
  Set_DatalogMetaData(data_log, char_num_str, 1); // DD-MM-YY
  //  sprintf(char_num_str, "l");
  //Set_DatalogMetaData(data_log, char_num_str, 1); // DD-MM-YY.l (implicit '.')
}
